import pandas as pd
import numpy as np


# # 热门视频数据处理
def host_move():
    open_cv1 = ['save1', 'save2']
    open_cv_num = [4, 5]

    for j in range(len(open_cv1)):
        data = []
        with open(f'data/{open_cv1[j]}.csv', 'r+', encoding='utf-8') as fp:
            file_data = fp.readlines()
            for i in file_data:
                data.append(i.replace('，', ','))
        # 数据规范化，去掉数据标题带的','
        with open(f'data/{open_cv1[j]}.csv', 'w', encoding='utf-8') as fp2:
            for i in data:
                if len(i.split(',')) > open_cv_num[j]:
                    num = len(i.split(',')) - open_cv_num[j]
                    fp2.write(i.replace(',', '，', num))
                else:
                    fp2.write(i)
            fp2.flush()
    print("去掉，成功")

    # 去掉‘分享’字样
    save_file = ["save3", "save4"]

    for m, n in enumerate(open_cv1):
        with open(f'data/{open_cv1[m]}.csv', 'r+', encoding='utf-8') as fp:
            data_1 = fp.readlines()
            with open(f'data/{save_file[m]}.csv', 'w', encoding='utf-8') as fp2:
                for i in data_1:
                    if (open_cv1[m] == "save1" and len(i.split(',')) == 4) or \
                            (open_cv1[m] == "save2" and len(i.split(',')) == 5):
                        for j in range(1, len(i.split(','))):
                            if open_cv1[m] == "save1" and j == 1:
                                pass
                            else:
                                if '亿' in i.split(',')[j]:
                                    num = str(float(i.split(',')[j].replace('亿', '')) * 100000000)
                                    i = i.replace(i.split(',')[j], num)
                                    if (open_cv1[m] == "save1" and j == 3) or (
                                            open_cv1[m] == "save2" and j == 4):
                                        i += "\n"
                                if open_cv1[m] == "save2" and '分享' in i.split(',')[4]:
                                    i = i.replace('分享', '0')
                                if open_cv1[m] == "save2" and '投币' in i.split(',')[2]:
                                    i = i.replace('投币', '0')
                        fp2.write(i)
                    fp2.flush()
    print("去掉干扰数据成功")

    b1 = pd.read_csv('data/save3.csv', sep=',', header=None)
    b2 = pd.read_csv('data/save4.csv', sep=',', header=None)

    b1.columns = ['标题', 'UP主', '播放量', '弹幕']
    b2.columns = ['标题', '点赞', '投币', '收藏', '转发']
    b3 = b1.merge(b2)
    b3[['播放量', '弹幕', '点赞', '投币', '收藏', '转发']] = b3[
        ['播放量', '弹幕', '点赞', '投币', '收藏', '转发']].astype('double')
    b3.to_csv('./flask_test/xlsx_data/ALLbilibili.csv', index=False)

    # 去除重复数据
    df = pd.read_csv("./flask_test/xlsx_data/ALLbilibili.csv")
    # print(df)
    df = df.drop_duplicates('标题')
    # print(df.reset_index())
    df.to_csv("./flask_test/xlsx_data/ALLbilibili.csv", index=False)
    df = pd.read_csv("./flask_test/xlsx_data/ALLbilibili.csv")
    df.to_csv("./flask_test/xlsx_data/ALLbilibili.csv")
    print("合并数据成功")


# 热门视频标签处理
def hot_type():
    list_type = pd.read_csv('data/list.csv', header=None)
    list_type.columns = ['类型']
    list_type['类型数量'] = 1
    list_type = list_type.groupby('类型').agg({'类型数量': 'sum'}).sort_values('类型数量',
                                                                         ascending=False).reset_index()
    list_type = list_type.loc[1:]
    # print(list_type)
    list_type.to_excel('./flask_test/xlsx_data/list_type.xlsx')


# 多元热度视频处理
def all_type_data():
    df_type_data = pd.read_csv('./data/second_type_data.csv', header=None)
    df_type_data = df_type_data.T
    df_type_data.columns = ["国创相关", "动画", "音乐", "舞蹈", "游戏", "知识", "科技", "运动", "汽车", "生活", "美食",
                            "动物圈", "鬼畜", "时尚", "娱乐",
                            "影视", "原创", "新人"]
    df_type_data = df_type_data.T
    df_type_data.columns = ['标签', '播放量', '评论', '点赞', '投币', '收藏', '转发']

    df_type_data.to_excel('./flask_test/xlsx_data/first_list_type_data.xlsx')


if __name__ == '__main__':
    host_move()
    # all_type_data()
    # hot_type()
